package com.hl.wuyeguanliIDEA.web;

import com.hl.wuyeguanliIDEA.pojo.UserYZ;
import com.hl.wuyeguanliIDEA.service.UserYZService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RestController
@RequestMapping("/api/userYZ")
@CrossOrigin // 解决跨域，生产环境按需配置
public class UserYZController {
    @Autowired
    private UserYZService userYZService;

    // 登录接口：返回包含id/name的用户信息
    @PostMapping("/login")
    public Result<UserYZ> login(@RequestBody LoginForm form) {
        UserYZ user = userYZService.login(form.getPhone(), form.getPassword());
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("手机号或密码错误");
    }

    // 获取用户信息接口
    @GetMapping("/info")
    public Result<UserYZ> getUserInfo(@RequestParam String phone) {
        UserYZ user = userYZService.getUserInfo(phone);
        if (user != null) {
            return Result.success(user);
        }
        return Result.error("用户信息获取失败");
    }

    // 修改密码接口
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody ChangePasswordForm form) {
        boolean success = userYZService.changePassword(form.getPhone(), form.getOldPassword(), form.getNewPassword());
        if (success) {
            return Result.success("密码修改成功");
        }
        return Result.error("旧密码错误或修改失败");
    }

    // 辅助类（可单独封装）
    static class LoginForm {
        private String phone;
        private String password;

        // Getter & Setter
        public String getPhone() { return phone; }
        public void setPhone(String phone) { this.phone = phone; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }

    static class ChangePasswordForm {
        private String phone;
        private String oldPassword;
        private String newPassword;

        // Getter & Setter
        public String getPhone() { return phone; }
        public void setPhone(String phone) { this.phone = phone; }
        public String getOldPassword() { return oldPassword; }
        public void setOldPassword(String oldPassword) { this.oldPassword = oldPassword; }
        public String getNewPassword() { return newPassword; }
        public void setNewPassword(String newPassword) { this.newPassword = newPassword; }
    }

    // 统一返回结果
    static class Result<T> {
        private int code = 200;
        private String msg;
        private T data;

        public static Result success(String msg) {
            Result result = new Result();
            result.setMsg(msg);
            return result;
        }

        public static <T> Result<T> success(T data) {
            Result<T> result = new Result<>();
            result.setData(data);
            return result;
        }

        public static Result error(String msg) {
            Result result = new Result();
            result.setCode(500);
            result.setMsg(msg);
            return result;
        }

        // Getter & Setter
        public int getCode() { return code; }
        public void setCode(int code) { this.code = code; }
        public String getMsg() { return msg; }
        public void setMsg(String msg) { this.msg = msg; }
        public T getDaata() { return data; }
        public void setData(T data) { this.data = data; }
    }
}